﻿using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Nasa8x.Mvc.Helpers.Attributes
{

    public class StripWhiteSpace : MemoryStream
    {

        private Stream _stream;
      
        private readonly HttpContextBase Context;


        public StripWhiteSpace(HttpContextBase _context)
        {
            Context = _context;
            _stream = _context.Response.Filter;
         //   _encode = _context.Request.Headers["Accept-Encoding"].ToUpperInvariant();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string _html = UTF8Encoding.UTF8.GetString(buffer);


            #region Temp
            //b = new Regex("\\t", RegexOptions.Compiled | RegexOptions.Multiline).Replace(b, string.Empty);
            //b = new Regex("&gt;\\r\\n<", RegexOptions.Compiled | RegexOptions.Multiline).Replace(b, "&gt;<");
            //b = new Regex("\\r\\n", RegexOptions.Compiled | RegexOptions.Multiline).Replace(b, string.Empty);

            //while (new Regex("  ", RegexOptions.Compiled | RegexOptions.Multiline).IsMatch(b))
            //{
            //    b = new Regex("  ", RegexOptions.Compiled | RegexOptions.Multiline).Replace(b, string.Empty);
            //}

            //b = new Regex("&gt;\\s<", RegexOptions.Compiled | RegexOptions.Multiline).Replace(b, "&gt;<");

            //_html = new Regex("(<pre>[^<>]*(((?<Open><)[^<>]*)+((?<Close-Open>>)[^<>]*)+)*(?(Open)(?!))</pre>)|\\s\\s+|[\\t\\n\\r]", RegexOptions.Compiled | RegexOptions.Singleline).Replace(_html, "$1");
            //_html = new Regex("<!--.*?-->", RegexOptions.Compiled | RegexOptions.Singleline).Replace(_html, string.Empty);

            //     private static readonly Regex _REGEX_BETWEEN_TAGS = new Regex(@">\s*<", RegexOptions.Singleline);
            //private static readonly Regex _REGEX_HTML_LINE_BREAKS = new Regex(@"\r\n", RegexOptions.Compiled);
            //private static readonly Regex _JAVASCRIPT_TAGS = new Regex(@"<script\s[^>]*>([\w\W]*?)</script>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            //private static readonly Regex _TEXTAREA_TAGS = new Regex(@"<textarea\s[^>]*>([\w\W]*?)</textarea>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            //private static readonly Regex _REGEX_TAB_SPACES = new Regex(@"\t*", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            #endregion

            
            //_html = new Regex("<!--.*?-->", RegexOptions.Compiled | RegexOptions.Singleline).Replace(_html, string.Empty);


            var _regexSpace = new Regex("  ", RegexOptions.Compiled | RegexOptions.Multiline);

            while (_regexSpace.IsMatch(_html))
            {
                _html = _regexSpace.Replace(_html, string.Empty);
            }

            _html = _html.Replace("\r", string.Empty).Replace("\t", string.Empty).Replace("\n", string.Empty);

            //_html = new Regex(@">\s*<", RegexOptions.Singleline).Replace(_html, "> <");



            _html += Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine +
                     Environment.NewLine + Environment.NewLine + Environment.NewLine;



            Context.Response.ContentEncoding = Encoding.UTF8;


            _stream.Write(UTF8Encoding.UTF8.GetBytes(_html), offset, UTF8Encoding.UTF8.GetByteCount(_html));
        }


    }

    public class HtmlOptimizeAttribute : ActionFilterAttribute
    {



        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {



            var o = filterContext.HttpContext.Response;
            o.Filter = new StripWhiteSpace(filterContext.HttpContext);



        }
    }


}
